package com.lrh.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lrh.reggie.common.BaseContext;
import com.lrh.reggie.common.R;
import com.lrh.reggie.dto.DishDto;
import com.lrh.reggie.entity.*;
import com.lrh.reggie.service.CategoryService;
import com.lrh.reggie.service.DishFlavorService;
import com.lrh.reggie.service.DishService;
import com.lrh.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * //新增套餐的添加菜品的数据回显  加上用户端  的对应的菜品分类得到对应菜品   后的版本
     *先发了这个请求  http://localhost:8080/shoppingCart/list
     *再发了这个  http://localhost:8080/dish/list?categoryId=1397844263642378242&status=1
     * 最终去执行了  DishController  的  list()方法
     * @param
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> shopCartList(){

        log.info("查看购物车。。。");

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        wrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> shoppingCartList = shoppingCartService.list(wrapper);

        return R.success(shoppingCartList);
    }


    /**
     * 添加菜品和套餐 到 购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){

        log.info("ShoppingCart:{}",shoppingCart);

        ShoppingCart newshoppingCart = shoppingCartService.addShoppingCart(shoppingCart);

        return R.success(newshoppingCart);
    }

    /**
     * 减少  购物车  的 菜品和套餐
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("ShoppingCart:{}",shoppingCart);

        ShoppingCart subShoppingCart = shoppingCartService.subShoppingCart(shoppingCart);

        return R.success(subShoppingCart);
    }

    @DeleteMapping("/clean")
    public R<String> clean(){

        log.info("清空购物车。。。");

        shoppingCartService.clean();

        return R.success("清空购物车成功");
    }

}
